package com.bocloud.cmp.server.controller.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.ApplyRecord;
import com.bocloud.cmp.interfaces.config.ApplyRecordService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

@RestController
@RequestMapping("/applyRecord")
public class ApplyRecordController {

	@Autowired
	private ApplyRecordService applyRecordService;

	/**
	 * 查询列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param simple
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return applyRecordService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple,
				user.getId());
	}

	/**
	 * 待审批的
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deal", method = { RequestMethod.POST })
	public BsmResult deal(@Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.deal(user.getId());
	}

	/**
	 * 已审批的
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param simple
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/done", method = { RequestMethod.POST })
	public BsmResult done(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return applyRecordService.done(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple,
				user.getId());
	}

	/**
	 * 详情
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
	public BsmResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.detail(id);
	}

	/**
	 * 待审批详情
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/{id}/approveDetail", method = { RequestMethod.GET })
	public BsmResult approveDetail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.approveDetail(id);
	}

	/**
	 * 通过
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/approve", method = { RequestMethod.POST })
	public BsmResult approve(@RequestParam(value = "applyId", required = true) Long applyId,
			@RequestParam(value = "nodeId", required = true) Long nodeId,
			@RequestParam(value = "reason", required = true) String reason, @Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.approve(applyId, nodeId, reason, user.getId());
	}

	/**
	 * 拒绝
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/refuse", method = { RequestMethod.POST })
	public BsmResult refuse(@RequestParam(value = "applyId", required = true) Long applyId,
			@RequestParam(value = "nodeId", required = true) Long nodeId,
			@RequestParam(value = "reason", required = true) String reason, @Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.refuse(applyId, nodeId, reason, user.getId());
	}

	/**
	 * 审批人
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/applyUser", method = { RequestMethod.POST })
	public BsmResult applyUser(@Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.applyUser();
	}

	/**
	 * 通过
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			ApplyRecord record = JSONObject.parseObject(object.toJSONString(), ApplyRecord.class);
			return applyRecordService.create(record, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 申请
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/{id}/apply", method = { RequestMethod.POST })
	public BsmResult apply(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = "content", required = true) String content,
			@RequestParam(value = "approvers", required = true) String approvers,
			@Value(Common.REQ_USER) RequestUser user) {
		return applyRecordService.apply(id, content, approvers, user.getId());
	}

}
